/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.dcr.az.vis.gui;

/**
 *
 * @author Shlomi
 */
import bgu.dcr.az.vis.presets.map.MapTester;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import simulator.statistics.Utility;
import simulator.testsimulator.TestSimulator;

public class VisGuiController {

    @FXML
    private Text actiontarget;
    @FXML
    private TextField agents;
    @FXML
    private TextField employment;
    @FXML
    private TextField ticks;
//    @FXML
//    private TextField seed;
    @FXML
    private TextField electric;
//    @FXML
//    private TextField p2;
    @FXML
    private Button runButton;
    @FXML
    private Button closeButton;
    @FXML
    private Button browseButton;
    @FXML
    private ComboBox<String> mapCombo;
    @FXML
    private ComboBox<String> chartCombo;

    final FileChooser fileChooser = new FileChooser();
    HashMap<String, Integer> titleToindex = new HashMap<>();
    public static boolean runClicked = false;

    @FXML
    protected void handleBrowseButtonAction(ActionEvent event) {
        Stage stage = (Stage) browseButton.getScene().getWindow();
        File file = fileChooser.showOpenDialog(stage);
        if (file != null) {
            openFile(file);
        }
    }

    @FXML
    protected void handleRunButtonAction(ActionEvent event) {
        if (runClicked) {
            actiontarget.setText("Already clicked run!");
            actiontarget.setVisible(true);
            return;
        }
        actiontarget.setVisible(false);
        actiontarget.setText("Bad values, try again!");
//        String mapText = mapCombo.getValue();
        int employment_ratio, agentnum, ticknum, elecnum;
        try {
//            seednum = Integer.parseInt(seed.getText());
            agentnum = Integer.parseInt(agents.getText());
            ticknum = Integer.parseInt(ticks.getText());
            elecnum = Integer.parseInt(electric.getText());
            employment_ratio = Integer.parseInt(employment.getText());
//            p2num = Double.parseDouble(p2.getText());
        } catch (NumberFormatException e) {
            actiontarget.setVisible(true);
            return;
        }
        if (elecnum > 100 || elecnum < 0 || employment_ratio > 100 || employment_ratio < 0 || agentnum < 0 || ticknum < 0) {
            actiontarget.setVisible(true);
            return;
        }

//        final String map = textToFileName(mapText);
        Utility.SIMULATOR_AGENTS = agentnum;
        Utility.SIMULATOR_TICKS = ticknum;
        Utility.SIMULATOR_ELECTRIC = (double)elecnum / 100.0;
        Utility.SIMULATOR_FILE = String.format("file_%da_%dt.bin", Utility.SIMULATOR_AGENTS, Utility.SIMULATOR_TICKS);
        Utility.SIMULATOR_EMPLOYMENT_RATIO = (double)employment_ratio / 100.0;
        try {
            //run simulation that also updates VisGUI.charts
            TestSimulator.main(new String[1]);

            //feed graph names
            int i = 0;
            for (XYChart<Number, Number> chart : VisGUI.charts) {
                chartCombo.getItems().add(chart.getTitle());
                titleToindex.put(chart.getTitle(), i);
                i++;
            }

//            MapTester.main(new String[1]);
            Platform.runLater(new Runnable() {
                public void run() {
                    try {
                        final Stage stage = new Stage();
                        runClicked = true;
                        new MapTester().start(stage);
                    } catch (Exception ex) {
                        Logger.getLogger(VisGuiController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });

            //            ProblemGenerator generator = new ProblemGenerator();
            //            final Problem problem = generator.generate(agentnum, ticknum, elecnum, p2num, seednum);
            ////            CPass1GUI.graphPane.setContent(new Label("Running..."));
            //            CPass1GUI.setOnPane(new Label("Running..."));
            //            AlgorithmRunningTask algoRunTask = new AlgorithmRunningTask(map, problem);
            //            algoRunTask.start();
        } catch (IOException ex) {
            Logger.getLogger(VisGuiController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @FXML
    private String textToFileName(String mapText) {
        String ans = null;
        if (mapText == null) {
            mapText = "nothing";
        }
        switch (mapText) {
            case "Tel-Aviv":
                ans = "graph_tlv.txt";
                break;
            case "Beer-Sheva":
                ans = "graph_beersheva.txt";
                break;
            case "Manhatten":
                ans = "graph_manhattan.txt";
                break;
            default:
                actiontarget.setVisible(true);
                break;
        }
        return ans;
    }

    @FXML
    protected void handleCloseButtonAction(ActionEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

    @FXML
    protected void handleChartChoice(ActionEvent event) {
//        int index = textToIndex(chartCombo.getValue());
        int index = titleToindex.get(chartCombo.getValue());
        VisGUI.setOnPane(VisGUI.charts.get(index));
    }

    @FXML
    protected void handleMapChoice(ActionEvent event) {
        String map = textToFileName(mapCombo.getValue());
        Utility.SIMULATOR_MAP = map;
    }

    private void openFile(File file) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
